package com.ych.modules.base.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ych.modules.api.authorInfc.raffleTicket.entity.QueryRaffleTicketEntity;
import com.ych.modules.base.dbEntity.TRaffleTicketJackpotEntity;
import com.ych.modules.base.dbEntity.TUserRaffleTicketEntity;
import com.ych.modules.base.mapper.TUserRaffleTicketMapper;
import com.ych.modules.base.service.ITRaffleTicketJackpotService;
import com.ych.modules.base.service.ITUserRaffleTicketService;
import com.ych.utils.beanToMap.BeanMapUtil;
import com.ych.utils.mysqlPageUtil.Query;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Author: Usopp.tsui
 * Date: 2020/12/15
 * Time: 11:04
 * Description:
 */
@Service("ITUserRaffleTicketService")
public class TUserRaffleTicketServiceImpl extends ServiceImpl<TUserRaffleTicketMapper, TUserRaffleTicketEntity> implements ITUserRaffleTicketService {
    @Autowired
    private ITRaffleTicketJackpotService itRaffleTicketJackpotService;

    @Override
    public Page<TRaffleTicketJackpotEntity> listRaffleTicket(QueryRaffleTicketEntity entity) {
        Wrapper<TRaffleTicketJackpotEntity> jackpotWrapper = new EntityWrapper<TRaffleTicketJackpotEntity>()
                .where("1=1")
                .orderBy("create_time desc");
        Page<TRaffleTicketJackpotEntity> page = itRaffleTicketJackpotService.selectPage(new Query<TRaffleTicketJackpotEntity>(BeanMapUtil.toPageQuery(entity)).getPage(), jackpotWrapper);
        for (TRaffleTicketJackpotEntity jackpotEntity : page.getRecords()) {
            Wrapper<TUserRaffleTicketEntity> wrapper = new EntityWrapper<TUserRaffleTicketEntity>()
                    .where("1=1")
                    .eq(StringUtils.isNotBlank(entity.getUserId()), "user_id", entity.getUserId())
                    .eq("jackpot_id", jackpotEntity.getJackpotId());
            List<TUserRaffleTicketEntity> list = this.selectList(wrapper);
            jackpotEntity.setRaffleTicketList(list);
        }
        return page;
    }
}